import VueRouter from "vue-router";
import pageFrame from "_c/pageFrame/pageFrame.vue";
import Token from "@/utils/token";


// 解决连续多次点击路由报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    component: pageFrame,
    children: [
      {
        path: "",
        name: "Home",
        meta: {
          navIndex: 0,
          title: "首页"
        },
        component: () => import("./views/home/home.vue")
      },
      {
        path: "dnbooking",
        name: "dnbooking",
        meta: {
          navIndex: 2,
          tabIndex: 0,
          title: "域名预订"
        },
        component: () => import("./views/dnBooking/dnBooking.vue")
      },
      {
        path: "bookdetail",
        name: "bookdetail",
        meta: {
          navIndex: 2,
          title: "预订详情"
        },
        component: () => import("./views/bookDetail/bookDetail.vue")
      },
      {
        path: "batchbooking",
        name: "batchbooking",
        meta: {
          navIndex: 2,
          tabIndex: 1,
          title: "批量预订"
        },
        component: () => import("./views/batchBooking/batchBooking.vue")
      },
      {
        path: "dnbidding",
        name: "dnbidding",
        meta: {
          navIndex: 3,
          tabIndex: 0,
          title: "域名竞价"
        },
        component: () => import("./views/dnBidding/dnBidding.vue")
      },
      {
        path: "biddingdetail",
        name: "biddingdetail",
        meta: {
          navIndex: 3,
          title: "竞价详情"
        },
        component: () => import("./views/biddingDetail/biddingDetail.vue")
      },
      {
        path: "dnre",
        name: "dnregistration",
        meta: {
          navIndex: 1,
          title: "域名注册"
        },
        component: () => import("./views/dnRegistration/dnRegistration.vue")
      },
      {
        path: "dnsr",
        name: "dnsearchresult",
        meta: {
          navIndex: 1,
          title: "搜索结果"
        },
        component: () => import("./views/dnRegistration/dnSearchResult.vue")
      },
      {
        path: "po",
        name: "placeorder",
        meta: {
          navIndex: 1,
          needLogin: true,
          title: "下单结算"
        },
        component: () => import("./views/dnRegistration/placeOrder.vue")
      },
      {
        path: "buyoutprice",
        name: "buyoutprice",
        meta: {
          navIndex: 4,
          tabIndex: 0,
          title: "一口价"
        },
        component: () => import("./views/buyoutPrice/buyoutPrice.vue")
      },
      {
        path: "buyoutdetail",
        name: "buyoutdetail",
        meta: {
          navIndex: 4,
          tabIndex: 0,
          title: "一口价详情"
        },
        component: () => import("./views/buyoutPrice/buyoutDetail.vue")
      },
      {
        path: "newsl",
        name: "newsl",
        meta: {
          title: "新闻资讯"
        },
        component: () => import("./views/news/newsList.vue")
      },
      {
        path: "newsd",
        name: "newsd",
        meta: {
          title: "新闻详情"
        },
        component: () => import("./views/news/newsDetail.vue")
      },
      {
        path: "rmregister",
        name: "rmregister",
        meta: {
          navIndex: 6,
          title: "商标注册"
        },
        component: () => import("./views/tradeMark/rmRegister.vue")
      },
      {
        path: "rmregisterf",
        name: "rmregisterf",
        meta: {
          navIndex: 6,
          title: "商标注册"
        },
        component: () => import("./views/tradeMark/rmRegisterForm.vue")
      },
      {
        path: "rmregisterm",
        name: "rmregisterm",
        meta: {
          navIndex: 6,
          title: "商标市场",
          tabIndex: 0
        },
        component: () => import("./views/tradeMark/rmRegisterMarket.vue")
      },
      {
        path: "rmdetail",
        name: "rmdetail",
        meta: {
          navIndex: 6,
          title: "商标详情",
          tabIndex: 0
        },
        component: () => import("./views/tradeMark/rmDetail.vue")
      },
      {
        path: "rmregisteri",
        name: "rmregisteri",
        meta: {
          navIndex: 6,
          title: "商标中介",
          tabIndex: 1
        },
        component: () => import("./views/tradeMark/rmRegisterIntermediary.vue")
      },
      {
        path: "dnpa",
        name: "dnPurchasingAgent",
        meta: {
          title: "委托代购",
          navIndex: 5,
        },
        component: () => import("./views/intermediary/dnPurchasingAgent.vue")
      },
      {
        path: "pointsmall",
        name: "pointsMall",
        meta: {
          navIndex: 8,
          title: "积分商城",
        },
        component: () => import("./views/pointsMall/pointsMall.vue")
      },
      {
        path: "pmdetail",
        name: "pmdetail",
        meta: {
          navIndex: 8,
          title: "商品详情",
        },
        component: () => import("./views/pointsMall/pmDetail.vue")
      },
      {
        path: "pmorder",
        name: "pmorder",
        meta: {
          navIndex: 8,
          title: "积分订单",
        },
        component: () => import("./views/pointsMall/pmOrder.vue")
      },
      {
        path: "dmsearch",
        name: "dmsearch",
        meta: {
          navIndex: 0,
          title: "域名搜索",
        },
        component: () => import("./views/home/dmSearch.vue")
      },
      {
        path: "whoissearch",
        name: "whoissearch",
        meta: {
          navIndex: 0,
          title: "whois搜索",
        },
        component: () => import("./views/home/whoisSearch.vue")
      },
      {
        path: "about",
        name: "about",
        meta: {
          title: "关于",
        },
        component: () => import("./views/home/about.vue")
      },
      {
        path: "apply",
        name: "apply",
        meta: {
          title: "申请代理",
        },
        component: () => import("./views/home/apply.vue")
      }
    ]
  },
  // 错误页面
  {
    path: "*",
    name: "error_404",
    meta: {
      title: "页面不存在"
    },
    component: () => import("@/mixins/error/404.vue")
  },
  {
    path: "*",
    name: "error_401",
    meta: {
      title: "没有访问权限"
    },
    component: () => import("@/mixins/error/401.vue")
  }
];



const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach(async (to, from, next) => {
  const url = window.location.hostname.replace('www.', '');
  // token相关
  const token = Token.get();
  // 路由排错
  if (to.matched.length === 0) {
    // 匹配前往的路由不存在
    from.name
      ? next({
        name: from.name
      })
      : next({
        name: "error_404"
      }); // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
  } else {
    if (!token && to.meta.needLogin) {
      window.location.href = "/login";
    } else {
      next();
      // 路由发生变化修改页面title
      if (to.meta.title) {
        document.title = `${to.meta.title} - `  + url;
      }
    }
  }
});

router.afterEach((to, from) => {
  if (to.name != from.name) {
    window.scrollTo(0, 0);
  }
});

export default router;
